package com.api.oa.controller;


import cn.hutool.core.util.StrUtil;
import com.api.oa.model.entity.OaNotice;
import com.api.oa.model.entity.OaOrg;
import com.api.oa.services.IOaOrgNoticeRelationService;
import com.api.oa.services.IOaOrgNoticeService;
import com.api.oa.services.IOaOrgService;
import com.api.shopapi.controller.BaseController;
import com.api.shopapi.entity.MyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 政府通知
 */
@RestController
@RequestMapping("/oa/org/notice")
public class OaOrgNoticeController extends BaseController {

    @Autowired
    private IOaOrgNoticeService iOaOrgNoticeService;


    @Autowired
    private IOaOrgNoticeRelationService iOaOrgNoticeRelationService;

    /**
     * 发布通知
     *
     * @param org
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody OaNotice org) {
        try {
            return success(iOaOrgNoticeService.add(org));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }

    /**
     * 发布者查看通知
     *
     * @param uid
     * @return
     */
    @RequestMapping(value = "/list/send", method = RequestMethod.GET)
    public MyResult listSend(@RequestParam(required = false) String uid,@RequestParam(defaultValue = "0") Long isDraft,@RequestParam Integer pageIndex,@RequestParam Integer pageSize) {
        try {
            return success(iOaOrgNoticeService.listFrom(uid,isDraft,pageIndex,pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }

    /**
     * 查看详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/list/{id}", method = RequestMethod.GET)
    public MyResult listDetail(@PathVariable Long id,@RequestParam Integer pageIndex,@RequestParam Integer pageSize) {
        try {
            return success(iOaOrgNoticeService.listUnderNotice(id,pageIndex,pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    /**
     * 我的通知
     *
     * @param uid
     * @param isRead
     * @return
     */
    @RequestMapping(value = "/list/receive", method = RequestMethod.GET)
    public MyResult listReceive(@RequestParam(required = false) String uid, @RequestParam(required = false) Integer isRead,@RequestParam(defaultValue = "1") Integer pageIndex,@RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            return success(iOaOrgNoticeService.listTo(uid, isRead,pageIndex,pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }

    /**
     * 我的通知设置已读
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/read/{id}", method = RequestMethod.GET)
    public MyResult readNotice(@PathVariable Long id) {
        try {
            iOaOrgNoticeService.readNotice(id);
            return success("已读!");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    /**
     * 删除公告  （发布人可以删除通知）
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/remove/{id}", method = RequestMethod.POST)
    public MyResult remove(@PathVariable Long id) {
        try {
            iOaOrgNoticeService.remove(id);
            return success("删除成功!");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "/one/{id}", method = RequestMethod.GET)
    public MyResult one(@PathVariable Long id) {
        try {
            return success(id == null ? null : iOaOrgNoticeService.one(id));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    /**
     *
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/relation/remove/{id}", method = RequestMethod.GET)
    public MyResult noticeReplay(@PathVariable Long id) {
        try {
             iOaOrgNoticeRelationService.remove(id);
            return  success("操作成功");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }

    /**
     * 附件回复（下发回复）
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/one/replay/{id}", method = RequestMethod.GET)
    public MyResult noticeReplay(@PathVariable Long id, @RequestParam(defaultValue = "") Long fileId) {
        try {
            if (StrUtil.isEmptyIfStr(fileId)) {
                return failed("回复文档不能为空");
            }
            Integer integer = iOaOrgNoticeService.noticeReplay(id, fileId);
            return integer == 1 ? success("操作成功") : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }

}
